using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Sec_scan漏洞扫描系统.Sec_Service
{
    /// <summary>
    /// 端口扫描服务
    /// </summary>
    public class Sec_PortScan_Service
    {
        /// <summary>
        /// 全端口扫描方法，用于先循环出所有的端口 大约 3 - 4秒能循环完成
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static int PortAll(int port)
        {
            Sec_Api.Sec_PortScan_Service_Api.PortInfoAll = new StringBuilder();//实例化后进行传出
            for(int i = 0;i <= 65535; i++)
            {
                Sec_Api.Sec_PortScan_Service_Api.PortInfoAll.Append(i.ToString());
            }
            return port;
        }
        /// <summary>
        /// 根据端口扫描配置来进行指定的端口扫描
        /// </summary>
        /// <param name="dispose"></param>
        /// <returns></returns>
        public static string PortDispose(string dispose)
        {
            return dispose;
        }
        /// <summary>
        /// TCP 端口扫描  执行的是纯tcp端口扫描
        /// <para>在执行纯tcp扫描的时候 默认采用的是多线程</para>
        /// </summary>
        /// <param name="tcp"></param>
        /// <returns></returns>
        public static string TcpScan(string tcp)
        {
            Task.Run(() =>
            {
                var tcps = new TcpClient();
                try
                {
                    tcps.Connect("192.168.1.6", 8008);
                    string ok = "连接成功！";
                }
                catch (Exception ex)
                {
                    string exp = ex.ToString();
                }
            });
            return tcp;
        }
        /// <summary>
        /// syn 端口扫描 这种方式扫描端口效果较好
        /// </summary>
        /// <param name="syn"></param>
        /// <returns></returns>
        public static string SynScan(string syn)
        {
            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
                socket.Connect(point);
                byte[] buffer = Encoding.ASCII.GetBytes("SYN");
                socket.Send(buffer);
                string ok = "连接成功！";
            }
            catch(Exception ex)
            {
                string exp = ex.ToString();
            }
            //int bytesReceived = socket.Receive(buffer);
            //string response = Encoding.ASCII.GetString(buffer, 0, bytesReceived);
            return syn; 
        }
        /// <summary>
        /// UDP 端口扫描
        /// </summary>
        /// <param name="udp"></param>
        /// <returns></returns>
        public static string UdpScan(string udp)
        {
            UdpClient client = new UdpClient();
            IPAddress remoteIP = IPAddress.Parse("10.60.4.53");  //假设发送给这个IP
            int remotePort = 11000;  //设置端口号
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);
            return udp;
        }
        /// <summary>
        /// icmp 协议探测
        /// </summary>
        /// <param name="icmp"></param>
        /// <returns></returns>
        public static string IcmpScan(string icmp)
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
            //设置远程服务器ip和端口
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
            //连接远程服务器
            clientSocket.Connect(serverEndPoint);
            //发送数据
            string sendData = "Hello World!";
            clientSocket.Send(System.Text.Encoding.Default.GetBytes(sendData)); 
            //接收数据
            byte[] receiveData = new byte[1024];
            int length = clientSocket.Receive(receiveData);
            string receiveDataStr = System.Text.Encoding.Default.GetString(receiveData, 0, length);
            Console.WriteLine("接收到的数据：" + receiveDataStr);

            //关闭连接
            clientSocket.Close();
            return icmp;
        }
        /// <summary>
        /// ACK 探测 ack 相对来讲也是比较快 在不发送数据的情况下
        /// </summary>
        /// <param name="ack"></param>
        /// <returns></returns>
        public static string AckScan(string ack)
        {
            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint point = new IPEndPoint(IPAddress.Parse("192.168.1.6"), 80);
                socket.Connect(point);
                byte[] buffer = Encoding.ASCII.GetBytes("ACK");
                socket.Send(buffer);
                Console.WriteLine(buffer.Length + "连接成功！");
                string ok = "连接成功！";
            }
            catch(Exception ex)
            {
                string exp = ex.ToString();
            }
            return ack;
        }
        /// <summary>
        /// 采用 fin 的方式对tcp 端口进行扫描
        /// </summary>
        /// <param name="fin"></param>
        /// <returns></returns>
        public static string FinScan(string fin)
        {
            return fin;
        }
    }
}
